<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="GBK">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>

<body>
  <div>fsagadasd</div>
  <script>
    // 捕获promise的reject
    // window.addEventListener('unhandledrejection', (e) => {
    //   console.log(e.reason);
    //   e.preventDefault()
    // })
    // 捕获常规错误、异步错误，到不能捕获到资源错误
    // window.onerror = function (message, source, lineno, colno, error) {
    //   console.log('捕获到的错误信息是：', message, source, lineno, colno, error)
    // }
    // // 可以监听到资源报错
    // window.addEventListener('error', (error) => {
    //   console.log('捕获到异常：', error);
    // }, true)
    function testFetch() {
      return new Promise((reslove, reject) => {
        setTimeout(() => {
          reject('testFetch')
        }, 1000)
      })
    }
    // setTimeout(() => {
    //   console.log(fdasfsda);
    // }, 1000)

    async function run() {
      testFetch().then(res => {
        console.log(res);
      })
    }
    run()
    window.addEventListener("load", () => {
      console.log("memory", performance.memory);
    });

    function init() {
      a()
    }
    function a() {
      console.log(1111);
      b()

    }
    function b() {
      throw new Error('t222est')
      console.log(2222);
      setTimeout(() => {

      })

      Promise.resolve().then(() => {
        console.log(222211111);
        throw new Error('te22st')
      })
    }
    try {
      init()
      console.log(33333);
    } catch (error) {
      console.log(1111, error, error.name);
      console.dir(error)
      window.errorSelf = error
    }
  </script>
  <script>
    const userConfig = {
      errorPlugin: true,
      monitorPlugin: true
    }

    function init(config) {
      const { plugins } = config

      Object.keys(plugins).forEach(key => {
        let name = plugins[key]

        // 动态创建script标签

        // 获取plugin代码

        // 将代码注册到内核实例中
      })
    }

    const FULL_MATCH =
      /^\s*at (?:(.*?) ?\()?((?:file|https?|blob|chrome-extension|address|native|eval|webpack|<anonymous>|[-a-z]+:|.*bundle|\/).*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i;

    // 限制只追溯10个
    const STACKTRACE_LIMIT = 10;

    // 解析每一行
    function parseStackLine(line) {
      const lineMatch = line.match(FULL_MATCH);
      if (!lineMatch) return {};
      const filename = lineMatch[2];
      const functionName = lineMatch[1] || '';
      const lineno = parseInt(lineMatch[3], 10) || undefined;
      const colno = parseInt(lineMatch[4], 10) || undefined;
      return {
        filename,
        functionName,
        lineno,
        colno,
      };
    }

    // 解析错误堆栈
    function parseStackFrames(error) {
      const { stack } = error;
      // 无 stack 时直接返回
      if (!stack) return [];
      const frames = [];
      for (const line of stack.split('\n').slice(1)) {
        const frame = parseStackLine(line);
        if (frame) {
          frames.push(frame);
        }
      }
      return frames.slice(0, STACKTRACE_LIMIT);
    }

    let bbb = parseStackFrames(window.errorSelf)
    console.log(bbb);


  </script>
  <!-- <script src="https://www.test.com/index.js" crossorigin></script> -->
</body>

</html>